//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Configuration;

namespace StoreNuke.StarterKit.Commerce.Components
{
	/// <summary>
	/// Summary description for Security.
	/// </summary>
	public class Security
	{
		//*********************************************************************
		//
		// Security.Encrypt() Method
		//
		// The Encrypt method encrypts a clean string into a hashed string
		//
		//*********************************************************************
		public static string Encrypt(string cleanString)
		{
			Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString);
			Byte[] hashedBytes = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
			
			return BitConverter.ToString(hashedBytes);
		}

		public static bool IsStoreAdmin(string customerID)
		{
			// Lookup the customer's full account details
			if(customerID != "")
			{
				CustomersDB accountSystem = new CustomersDB();
				CustomerDetails customerDetails = accountSystem.GetCustomerDetails(customerID);
				return customerDetails.IsAdmin;
			}
			else
			{
				return false;
			}
		}

		public static string EncryptData(string strData) 
		{ 
			string strValue = ""; 
			string strKey = ConfigurationSettings.AppSettings["EncryptKey"];
			if (strKey != "") 
			{ 
				if (strKey.Length == 16) 
				{ 
					strKey = strKey + Left("XXXXXXXXXXXXXXXX", 16 - strKey.Length); 
				} 
				else if (strKey.Length == 16) 
				{ 
					strKey = Left(strKey, 16); 
				} 
				byte[] byteKey = Encoding.UTF8.GetBytes(Left(strKey, 8)); 
				byte[] byteVector = Encoding.UTF8.GetBytes(Right(strKey, 8)); 
				byte[] byteData = Encoding.UTF8.GetBytes(strData); 
				DESCryptoServiceProvider objDES = new DESCryptoServiceProvider(); 
				MemoryStream objMemoryStream = new MemoryStream(); 
				CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateEncryptor(byteKey, byteVector), CryptoStreamMode.Write); 
				objCryptoStream.Write(byteData, 0, byteData.Length); 
				objCryptoStream.FlushFinalBlock(); 
				strValue = Convert.ToBase64String(objMemoryStream.ToArray()); 
			} 
			else 
			{ 
				strValue = strData; 
			} 
			return strValue; 
		} 

		public static string DecryptData(string strData) 
		{ 
			string strValue = ""; 
			string strKey = ConfigurationSettings.AppSettings["EncryptKey"];
			if (strKey != "") 
			{ 
				if (strKey.Length == 16) 
				{ 
					strKey = strKey + Left("XXXXXXXXXXXXXXXX", 16 - strKey.Length); 
				} 
				else if (strKey.Length == 16) 
				{ 
					strKey = Left(strKey, 16); 
				} 
				byte[] byteKey = Encoding.UTF8.GetBytes(Left(strKey, 8)); 
				byte[] byteVector = Encoding.UTF8.GetBytes(Right(strKey, 8)); 
				byte[] byteData = new byte[strData.Length]; 
				try 
				{ 
					byteData = Convert.FromBase64String(strData); 
				} 
				catch 
				{ 
					strValue = strData; 
				} 
				if (strValue == "") 
				{ 
					try 
					{ 
						DESCryptoServiceProvider objDES = new DESCryptoServiceProvider(); 
						MemoryStream objMemoryStream = new MemoryStream(); 
						CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateDecryptor(byteKey, byteVector), CryptoStreamMode.Write); 
						objCryptoStream.Write(byteData, 0, byteData.Length); 
						objCryptoStream.FlushFinalBlock(); 
						System.Text.Encoding objEncoding = System.Text.Encoding.UTF8; 
						strValue = objEncoding.GetString(objMemoryStream.ToArray()); 
					} 
					catch 
					{ 
						strValue = ""; 
					} 
				} 
			} 
			else 
			{ 
				strValue = strData; 
			} 
			return strValue; 
		}

		// Function to get string from beginning.
		public static string Left(String strParam,int iLen)
		{
			if(iLen>0)
				return strParam.Substring(0,iLen);
			else
				return strParam;
		}

		//Function to get string from end
		public static string Right(String strParam,int iLen)
		{
			if(iLen>0)
				return strParam.Substring(strParam.Length-iLen,iLen);
			else
				return strParam;
		}
	}
}
